/**
 * cmds.c - user /commands for gay.com
 *
 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
 * Copyright (C) 2008, Daniel J Reidy <dubkat@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: cmds.c 128 2008-04-13 01:34:40Z dubkat $
 */


#include "conversation.h"
#include "debug.h"
#include "notify.h"
#include "util.h"
#include "helpers.h"
#include "pecker.h"

int pecker_get_info(PurpleConnection *, const char *);

/**
 * TODO: trim the fat off this file, there are too many unsupported /commands carried over from the IRC module. -dubkat
 */

//static void pecker_do_mode(struct pecker_conn *pecker, const char *target, const char *sign, char **ops);
/**
int
pecker_cmd_default(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, target,
                                                                      pecker->account);
    char *buf;

    if(!convo)
        return 1;

    buf = g_strdup_printf("Unknown command: %s", cmd);
    if(purple_conversation_get_type(convo) == PURPLE_CONV_TYPE_IM)
        purple_conv_im_write(PURPLE_CONV_IM(convo), "", buf, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LOG, time(NULL));
    else
        purple_conv_chat_write(PURPLE_CONV_CHAT(convo), "", buf, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LOG, time(NULL));
    g_free(buf);

    return 1;
}
*/

int
pecker_cmd_away(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf, *message, *cur;

    if(args[0] && strcmp(cmd, "back"))
    {
        message = strdup(args[0]);
        for (cur = message; *cur; cur++)
        {
            if(*cur == '\n')
                *cur = ' ';
        }
        buf = pecker_format(pecker, "v:", "AWAY", message);
        g_free(message);
    }
    else
    {
        buf = pecker_format(pecker, "v", "AWAY");
    }
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

int
pecker_cmd_ctcp_action(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    PurpleConnection *gc = purple_account_get_connection(pecker->account);
    char *action, *dst, **newargs;
    const char *src;
    PurpleConversation *convo;

    if(!args || !args[0] || !gc)
        return 0;

    action = g_malloc(strlen(args[0]) + 10);

    sprintf(action, "\001ACTION ");

    src = args[0];
    dst = action + 8;
    while (*src)
    {
        if(*src == '\n')
        {
            if(*(src + 1) == '\0')
            {
                break;
            }
            else
            {
                *dst++ = ' ';
                src++;
                continue;
            }
        }
        *dst++ = *src++;
    }
    *dst++ = '\001';
    *dst = '\0';

    newargs = g_new0(char *, 2);

    newargs[0] = g_strdup(target);
    newargs[1] = action;
    pecker_cmd_privmsg(pecker, cmd, target, (const char **) newargs);
    g_free(newargs[0]);
    g_free(newargs[1]);
    g_free(newargs);

    convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, target, pecker->account);
    if(convo)
    {
        action = g_strdup_printf("/me %s", args[0]);
        if(action[strlen(action) - 1] == '\n')
            action[strlen(action) - 1] = '\0';
        if(purple_conversation_get_type(convo) == PURPLE_CONV_TYPE_CHAT)
            serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), purple_connection_get_display_name(gc), 0,
                             action, time(NULL));
        else
            purple_conv_im_write(PURPLE_CONV_IM(convo), purple_connection_get_display_name(gc), action, 0, time(NULL));
        g_free(action);
    }

    return 1;
}

int
pecker_cmd_trace(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    buf = pecker_format(pecker, "vn", "TRACE", args[0]);
    pecker_send(pecker, buf);
    g_free(buf);

    pecker->traceconv = g_strdup(target);

    return 0;
}

int
pecker_cmd_invite(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{

    char *buf;

    if(!args || !args[0] || !(args[1] || target))
        return 0;

    buf = pecker_format(pecker, "vnc", "INVITE", args[0], args[1] ? args[1] : target);
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}


int
pecker_cmd_join(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args || !args[0])
        return 0;

    buf = pecker_format(pecker, "cv", "JOIN", args[0]);

    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

/* you cannot kick users on gay.com. why is this even here? -dubkat */
/**
int
pecker_cmd_kick(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
  
       char *buf;
       PurpleConversation *convo;

       if(!args || !args[0])
       return 0;

       convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, target, pecker->account);
       if(!convo)
       return 0;

       if(args[1])
       buf = pecker_format(pecker, "vcn:", "KICK", target, args[0], args[1]);
       else
       buf = pecker_format(pecker, "vcn", "KICK", target, args[0]);
       pecker_send(pecker, buf);
       g_free(buf);
  
      return 0;
}
*/

int
pecker_cmd_list(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    if(args[0])
    {
        gchar *tmp = g_utf8_strdown(args[0], -1);

        pecker->roomlist_filter = g_utf8_normalize(tmp, -1, G_NORMALIZE_ALL);
        g_free(tmp);
    }
    else
    {
        pecker->roomlist_filter = NULL;
    }
    purple_roomlist_show_with_account(pecker->account);

    return 0;
}

/* there are no luser settable modes on gay.com. -dubkat */
/**
int
pecker_cmd_mode(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{

    PurpleConnection *gc;
    char *buf;

    if(!args)
        return 0;

    if(!strcmp(cmd, "mode"))
    {
        if(!args[0] && (*target == '#' || *target == '&'))
            buf = pecker_format(pecker, "vc", "MODE", target);
        else if(args[0] && (*args[0] == '+' || *args[0] == '-'))
            buf = pecker_format(pecker, "vcv", "MODE", target, args[0]);
        else if(args[0])
            buf = pecker_format(pecker, "vv", "MODE", args[0]);
        else
            return 0;
    }
    else if(!strcmp(cmd, "umode"))
    {
        if(!args[0])
            return 0;
        gc = purple_account_get_connection(pecker->account);
        buf = pecker_format(pecker, "vnv", "MODE", purple_connection_get_display_name(gc), args[0]);
    }
    else
    {
        return 0;
    }

    pecker_send(pecker, buf);
    g_free(buf);
    return 0;
}
*/

/* i dont see the point of this, and it causes a crash. lets just get rid of it */
/**
int
pecker_cmd_names(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args)
        return 0;

    buf = pecker_format(pecker, "vc", "NAMES", args[0] ? args[0] : target);
    pecker_send(pecker, buf);
    g_free(buf);

    pecker->nameconv = g_strdup(target);

    return 0;
}
*/

/* you can't change your nick, but you can change it's case. */
int
pecker_cmd_nick(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;
    char *temp;

    if(!args || !args[0])
        return 0;

    temp = pecker_nick_to_gcom_strdup(args[0]);
    buf = pecker_format(pecker, "v:", "NICK", temp);
    pecker_send(pecker, buf);
    g_free(buf);
    g_free(temp);
    return 0;
}

/* yeah, as if */
/**
int
pecker_cmd_op(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{

    char **nicks, **ops, *sign, *mode;
    int i = 0, used = 0;

    if(!args || !args[0] || !*args[0])
        return 0;

    if(!strcmp(cmd, "op"))
    {
        sign = "+";
        mode = "o";
    }
    else if(!strcmp(cmd, "deop"))
    {
        sign = "-";
        mode = "o";
    }
    else if(!strcmp(cmd, "voice"))
    {
        sign = "+";
        mode = "v";
    }
    else if(!strcmp(cmd, "devoice"))
    {
        sign = "-";
        mode = "v";
    }
    else
    {
        purple_debug(PURPLE_DEBUG_ERROR, "pecker", "invalid 'op' command '%s'\n", cmd);
        return 0;
    }

    nicks = g_strsplit(args[0], " ", -1);

    ops = g_new0(char *, i * 2 + 1);

    for (i = 0; nicks[i]; i++)
    {
        if(!*nicks[i])
            continue;
        ops[used++] = mode;
        ops[used++] = nicks[i];
    }

    pecker_do_mode(pecker, target, sign, ops);
    g_free(ops);

    return 0;
}
*/

int
pecker_cmd_part(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args)
        return 0;

    if(args[1])
        buf = pecker_format(pecker, "vc:", "PART", args[0] ? args[0] : target, args[1]);
    else
        buf = pecker_format(pecker, "vc", "PART", args[0] ? args[0] : target);
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}


int
pecker_cmd_ping(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *stamp;
    char *buf;

    if(args && args[0])
    {
        if(*args[0] == '#' || *args[0] == '&')
            return 0;
        stamp = g_strdup_printf("\001PING %lu\001", time(NULL));
        buf = pecker_format(pecker, "vn:", "PRIVMSG", args[0], stamp);
        g_free(stamp);
    }
    else
    {
        stamp = g_strdup_printf("%s %lu", target, time(NULL));
        buf = pecker_format(pecker, "v:", "PING", stamp);
        g_free(stamp);
    }
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

int
pecker_cmd_privmsg(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    const char *cur, *end;
    char *msg, *buf, *nick;
    gchar *unescaped;

    if(!args || !args[0] || !args[1])
        return 0;

    /**
     * Only run pecker_nick_to_gcom_strdup() against nicks,
     * never on channels (which begin with either "#" or "&")
     */
    if(args[0][0] != '#' && args[0][0] != '&')
    {
        nick = pecker_nick_to_gcom_strdup(args[0]);
    }
    else
    {
        nick = g_strdup(args[0]);
    }
    unescaped = purple_markup_strip_html(args[1]);

    cur = unescaped;
    end = unescaped;
    while (*end && *cur)
    {
        end = strchr(cur, '\n');
        if(!end)
            end = cur + strlen(cur);
        msg = g_strndup(cur, end - cur);

        buf = pecker_format(pecker, "vt:", "PRIVMSG", nick, msg);
        purple_debug_misc("pecker_cmd_privmsg", buf);
        pecker_send(pecker, buf);
        g_free(msg);
        g_free(buf);
        cur = end + 1;
    }
    g_free(nick);
    g_free(unescaped);
    return 0;
}

int
pecker_cmd_quit(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!pecker->quitting)
    {
        buf = pecker_format(pecker, "v:", "QUIT", (args && args[0]) ? args[0] : "Leaving");
        pecker_send(pecker, buf);
        g_free(buf);

        pecker->quitting = TRUE;
    }

    return 0;
}

int
pecker_cmd_quote(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args || !args[0])
        return 0;

    buf = pecker_format(pecker, "v", args[0]);
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

int
pecker_cmd_query(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    PurpleConversation *convo;
    PurpleConnection *gc;

    if(!args || !args[0])
        return 0;

    convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, pecker->account, args[0]);

    if(args[1])
    {
        gc = purple_account_get_connection(pecker->account);
        pecker_cmd_privmsg(pecker, cmd, target, args);
        purple_conv_im_write(PURPLE_CONV_IM(convo), purple_connection_get_display_name(gc), args[1], PURPLE_MESSAGE_SEND,
                             time(NULL));
    }

    return 0;
}

/* no clue what this is doing */
int
pecker_cmd_remove(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args || !args[0])
        return 0;

    if(*target != '#' && *target != '&')        /* not a channel, punt */
        return 0;

    if(args[1])
        buf = pecker_format(pecker, "vcn:", "REMOVE", target, args[0], args[1]);
    else
        buf = pecker_format(pecker, "vcn", "REMOVE", target, args[0]);
    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

/* riiiiight. */
/**
int
pecker_cmd_topic(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;
    const char *topic;
    PurpleConversation *convo;

    if(!args)
        return 0;

    convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, target, pecker->account);
    if(!convo)
        return 0;

    if(!args[0])
    {
        topic = purple_conv_chat_get_topic(PURPLE_CONV_CHAT(convo));

        if(topic)
        {
            char *tmp, *tmp2;

            tmp = g_markup_escape_text(topic, -1);
            tmp2 = purple_markup_linkify(tmp);
            buf = g_strdup_printf("current topic is: %s", tmp2);
            g_free(tmp);
            g_free(tmp2);
        }
        else
            buf = g_strdup("No topic is set");
        purple_conv_chat_write(PURPLE_CONV_CHAT(convo), target, buf, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LOG, time(NULL));
        g_free(buf);

        return 0;
    }

    buf = pecker_format(pecker, "vt:", "TOPIC", target, args[0]);
    pecker_send(pecker, buf);
    g_free(buf);
    return 0;
}
**/

/* ORLY? */
int
pecker_cmd_wallops(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{

    char *buf;

    if(!args || !args[0])
        return 0;

    if(!strcmp(cmd, "wallops"))
        buf = pecker_format(pecker, "v:", "WALLOPS", args[0]);
    else if(!strcmp(cmd, "operwall"))
        buf = pecker_format(pecker, "v:", "OPERWALL", args[0]);
    else
        return 0;

    pecker_send(pecker, buf);
    g_free(buf);

    return 0;
}

/* instead of sending a raw WHOIS, which does nothing for us
 * make a biobox pop up!
 */

int
pecker_cmd_info(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *converted_nick;

    // pecker_get_info(PurpleConnection * gc, const char *who)

    PurpleConnection *gc = purple_account_get_connection(pecker->account);

    if(!args || !args[0])
        return 0;

    converted_nick = pecker_nick_to_gcom_strdup(args[0]);
    //buf = pecker_format(pecker, "vn", "WHOIS", converted_nick);
    purple_debug_misc("pecker_cmd_info", "Attempting to lookup UserInfo for %s\n", converted_nick);

    pecker_get_info(gc, args[0]);
    g_free(converted_nick);
    return 0;
}


int
pecker_cmd_whois(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;
    char *converted_nick;

    if(!args || !args[0])
        return 0;

    converted_nick = pecker_nick_to_gcom_strdup(args[0]);
    buf = pecker_format(pecker, "vn", "WHOIS", converted_nick);
    pecker_send(pecker, buf);
    g_free(buf);
    g_free(converted_nick);
    return 0;
}


int
pecker_cmd_who(struct pecker_conn *pecker, const char *cmd, const char *target, const char **args)
{
    char *buf;

    if(!args || !args[0])
        return 0;

    buf = pecker_format(pecker, "vn", "WHO", args[0]);
    purple_debug_misc("cmds", "Exceuting %s\n", buf);
    pecker_send(pecker, buf);
    g_free(buf);
    return 0;
}

/* SERIOUSLY NOW! what do you think this is... IRC?!?! */
/**
static void
pecker_do_mode(struct pecker_conn *pecker, const char *target, const char *sign, char **ops)
{

    char *buf, mode[5];
    int i = 0;

    if(!sign)
        return;

    while (ops[i])
    {
        if(ops[i + 2] && ops[i + 4])
        {
            g_snprintf(mode, sizeof(mode), "%s%s%s%s", sign, ops[i], ops[i + 2], ops[i + 4]);
            buf = pecker_format(pecker, "vcvnnn", "MODE", target, mode, ops[i + 1], ops[i + 3], ops[i + 5]);
            i += 6;
        }
        else if(ops[i + 2])
        {
            g_snprintf(mode, sizeof(mode), "%s%s%s", sign, ops[i], ops[i + 2]);
            buf = pecker_format(pecker, "vcvnn", "MODE", target, mode, ops[i + 1], ops[i + 3]);
            i += 4;
        }
        else
        {
            g_snprintf(mode, sizeof(mode), "%s%s", sign, ops[i]);
            buf = pecker_format(pecker, "vcvn", "MODE", target, mode, ops[i + 1]);
            i += 2;
        }
        pecker_send(pecker, buf);
        g_free(buf);
    }


    return;
}
*/

/**
 * vim:tabstop=4:shiftwidth=4:expandtab:
 */
